@extends('public.dashboard_base')
@section('title', '批量解析')

@section('content')
    <div class="control_panel_content">
        <div class="navBreadcrumb">
            <ul>
                <li>域名管理</li>
                <li><a href="/domain/plcz">批量操作</a></li>
                <li><a href="/domain/plcz/jiexi">批量解析</a></li>
            </ul>
        </div>
        <div class="panel">
            <div class="sub_nav_container">
                <ul class="clearfix">
                    <li class="active"><a href="/domain/plcz/jiexi">批量解析</a></li>
                    <!--<li><a href="?ym_dns/pledit">批量修改记录</a></li>
                    <li><a href="?ym_dns/pldel">批量删除记录</a></li>-->
                </ul>
            </div>
            <div class="panel_body">
                <form id="record_form" action="" method="post">
                    <input type="hidden" name="operation" value="ADD">
                    <input type="hidden" name="count" id="count" value="0">
                    <ul class="form_group">
                        <li>
                            <span class="form_label">域名</span>
                            <textarea name="domain" id="domain" placeholder="每行一个域名，一次最多输入1000个" class="domain_area" autofocus=""> </textarea>
                            <span class="text_light_gray">&emsp;您已经填写<em class="text_orange" id="entered-count">0</em>个域名，还可添加<em class="text_orange" id="remaining-count">1000</em>个</span>
                        </li>
                        <li>
                            <span class="form_label">记录</span>
                            <input type="text" name="record0.host" placeholder="主机记录如：www" style="width:120px;">
                            <select name="record0.type" class="record_type">
                                <option value="">记录类型</option>
                                <option value="A">A</option>
                                <option value="CNAME">CNAME</option>
                                <option value="MX">MX</option>
                                <option value="TXT">TXT</option>
                                <option value="AAAA">AAAA</option>
                            </select>
                            <select name="record0.line">
                                <option value="默认">默认</option>
                                <option value="电信">电信</option>
                                <option value="联通">联通</option>
                                <option value="移动">移动</option>
                            </select>
                            <input type="text" name="record0.value" placeholder="记录值" style="width: 200px;">
                            <!--<input type="text" name="record0.weight" readonly="" placeholder="MX/权重"
                                   class="record_mx" style="width: 100px;">-->
                            <input type="text" name="record0.ttl" placeholder="TTL" value="600" style="width: 100px;">&ensp;
                            <a href="javascript:;" id="add-record">
                                <i class="iconfont iconjia" style="font-size: 12px;"></i>
                                增加一行
                            </a>
                        </li>
                        <li class="clear_record">
                            <span class="form_label">&nbsp;</span>
                            <label>
                                <input type="checkbox" name="deleteOrigin" value="1">
                                清空目标域名原有的解析记录
                                <span class="text_light_gray" style="margin-left: 40px;">注意：勾选该项后，目标域名原有解析记录将被清空</span>
                            </label>
                        </li>
                        <li>
                            <span class="form_label">&nbsp;</span>
                            <input type="button" class="btn btn_primary" id="Btn111" value="提交">
                        </li>
                    </ul>
                </form>
            </div>
            <div class="batch_operation_result" id="batch-result" style="display:none">
            </div>
        </div>
    </div>
@endsection
@section('script')
    <script>
        $(function () {
            var link = $("#domain-plcz");
            link.parent().parent().parent().addClass("active");
            link.parent().addClass("active");
        });

        //复制
        function copy(that) {
            var list = $(that).closest('.fail').find('dd');
            var arr = new Array();
            list.each(function () {
                arr.push($(this).html());
            });
            var area = $('<textarea>').val(arr.join("\n"));
            area.appendTo($('body'));
            area.select();
            document.execCommand('copy');
            layer.msg('复制成功', {
                time: 1000
            });
            area.remove();
        }

        $(function () {

            // 输入域名数量
            var domainCount = 0;
            var remainingCount = 0;
            $('.domain_area').on('keyup paste cut', function (e) {
                clearTimeout(this.timer);
                var that = this;
                this.timer = setTimeout(function () {
                    var domainArr = removeEmpty($(that).val().split('\n'));
                    domainCount = domainArr.length;
                    remainingCount = domainCount < 1000 ? (1000 - domainCount) : 0;
                    $('#entered-count').html(domainCount);
                    $('#remaining-count').html(remainingCount);
                }, 500);
            });

            $('.domain_area').trigger('keyup');
            $('#Btn111').click(function () {
                var snum = $('#count').val();
                var jx = new Array();
                if (snum>0){
                    for (var i = 0; i <= snum; i++) {
                        var host = $("input[name='record"+i+".host']").val();
                        var line = $("select[name='record"+i+".line']").val();
                        var value = $("input[name='record"+i+".value']").val();
                        var type = $("select[name='record"+i+".type']").val();
                        jx[i] = host + '|' + type + '|' + value + '|' + line;
                    }
                }else{
                    var host = $("input[name='record0.host']").val();
                    var line = $("select[name='record0.line']").val();
                    var value = $("input[name='record0.value']").val();
                    var type = $("select[name='record0.type']").val();
                    jx[0] = host + '|' + type + '|' + value + '|' + line;
                }

                var domain=$('#domain').val();
                var qk= $('input:checkbox[name=deleteOrigin]:checked').val();
                if (domainCount > 1000) {
                    layer.msg("最多只能提交1000个域名", {
                        time: 2000
                    });
                } else {
                    //  console.log(jx);return false;
                    $.ajax({
                        url:'/ym_dns/plcreatedns',
                        data: {domain:domain,jx:jx,qk:qk},
                        dataType:"json",
                        type:"post",
                        success:function(res){
                            if(res.code==1){
                                layer.msg(res.msg);
                                setTimeout(function(){
                                    // loadingPage();
                                    window.location.reload();
                                },2000);
                            }else{
                                layer.msg(res.msg);
                            }
                        }
                    });
                }
            })

            /* $("#record_form").submit(function () {
                var snum = $('#count').val();
                 var jx = new Array();
                 if (snum>0){
                     for (var i = 0; i <= snum; i++) {
                         var host = $("input[name='record"+i+".host']").val();
                         var line = $("select[name='record"+i+".line']").val();
                         var value = $("input[name='record"+i+".value']").val();
                         var type = $("select[name='record"+i+".type']").val();
                         jx[i] = host + '|' + type + '|' + value + '|' + line;
                     }
                 }else{
                     var host = $("input[name='record0.host']").val();
                     var line = $("select[name='record0.line']").val();
                     var value = $("input[name='record0.value']").val();
                     var type = $("select[name='record0.type']").val();
                     jx[0] = host + '|' + type + '|' + value + '|' + line;
                 }

                 var domain=$('#domain').val();
                 var qk= $('input:checkbox[name=deleteOrigin]:checked').val();
                 if (domainCount > 1000) {
                     layer.msg("最多只能提交1000个域名", {
                         time: 2000
                     });
                 } else {
                     $(this).ajaxSubmit({
                         data: {domain:domain,jx:jx,qk:qk},
                        /!* data: $("#record_form").serialize(),*!/
                         dataType: "json",
                         beforeSubmit: function (formData, jqForm, options) {
                             layer.load(2)
                         },
                         success: function (data) {
                             layer.closeAll("loading");
                             if (data.code == "1") {
                                 $('#submitBtn').prop('disabled', true);
                                 $('#batch-result').html(data.submitResult).show();
                             } else {
                                 if (data.submitResult) {
                                     $('#submitBtn').prop('disabled', true);
                                     $('#batch-result').html(data.submitResult).show();
                                 } else if (data.msg) {
                                     layer.msg(data.msg);
                                 } else {
                                     formValidateTips(data, "#record_form")
                                 }
                             }
                         },
                         error: function (ret) {
                         },
                         complete: function (ret) {
                             layer.closeAll("loading")
                         }
                     });
                 }
                 return false;
             });
     */
            //增加记录
            var count = 0;
            $('#add-record').on('click', function () {
                count++;
                var li = '<li><span class="form_label">&ensp;</span>&nbsp;<input type="text" name="record' +
                    count + '.host" placeholder="主机记录如：www" style="width:120px;">&nbsp;<select name="record' +
                    count +
                    '.type" class="record_type"><option value="">记录类型</option><option value="A">A</option><option value="CNAME">CNAME</option><option value="MX">MX</option><option value="TXT">TXT</option><option value="AAAA">AAAA</option></select>&nbsp;<select name="record' +
                    count +
                    '.line"><option value="默认">默认</option><option value="电信">电信</option><option value="联通">联通</option><option value="移动">移动</option></select>&nbsp;<input type="text" name="record' +
                    count +
                    '.value" placeholder="记录值" style="width: 200px;">&nbsp;<input type="text" name="record' +
                    count +
                    '.ttl" placeholder="TTL" value="600" style="width: 100px;">&ensp;&nbsp;<a href="javascript:;" class="del_record"><i class="iconfont iconcha" style="font-size: 12px;"></i></a></li>';
                $('.clear_record').before(li);
                $('#count').val(count);
            });

            //删除
            $(document).on('click', '.del_record', function () {
                var countj = $('#count').val();
                $(this).closest('li').fadeOut(function () {
                    $(this).remove();
                    countj--;
                    $('#count').val(countj);
                });
            });

            //判断mx
            $(document).on('change', '.record_type', function () {
                var val = $(this).val();
                var mx = $(this).closest('li').find('.record_mx');
                if (val == "MX") {
                    mx.removeAttr("readonly");
                } else {
                    mx.attr("readonly", "readonly");
                }
            });
        })
    </script>
@endsection
